import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
    routes: [{
            path: '/',
            redirect: '/dwnCenter'
        },
        {
            path: '/',
            component: resolve => require(['../components/common/Home.vue'], resolve),
            meta: {
                title: '自述文件'
            },
            children: [{
                    path: '/dwnCenter',
                    component: resolve => require(['../components/page/dwnCenter.vue'], resolve),
                    meta: {
                        title: '系统首页'
                    }
                },
                {
                    path: '/icon',
                    component: resolve => require(['../components/page/Icon.vue'], resolve),
                    meta: {
                        title: '自定义图标'
                    }
                },
                {
                    path: '/userInfo',
                    component: resolve => require(['../components/page/UserInfo.vue'], resolve),
                    meta: {
                        title: '个人中心'
                    }
                },
                {

                    path: '/rechargeIntegral',
                    component: resolve => require(['../components/page/rechargeIntegral.vue'], resolve),
                    meta: {
                        title: '兑换专区'
                    }

                },
                {
                    path: '/404',
                    component: resolve => require(['../components/page/404.vue'], resolve),
                    meta: {
                        title: '404'
                    }
                },
                {
                    path: '/403',
                    component: resolve => require(['../components/page/403.vue'], resolve),
                    meta: {
                        title: '403'
                    }
                }
            ]
        },
        {
            path: '/login',
            component: resolve => require(['../components/page/Login.vue'], resolve)
        },
        {
            path: '*',
            redirect: '/404'
        }
    ]
})
// 路由导航守卫
router.beforeEach((to, from, next) => {
    console.log(to, from)
    // 请求login放行
    if (to.path === '/login') return next()
    // 拿到登陆的token
    const tokenStr = Vue.cookie.get('token')
    // 没有token跳回login
    if (!tokenStr) return next('/login')
    next()
})
export default router
